﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.Core.BLL
{
    public class ImagesListView
    {
        public int ImageId { get; set; }
        public string ImageName { get; set; }
        public string ImageDesc { get; set; }
        public string ImageUrl { get; set; }
        public DateTime ImageDate { get; set; }
        public string AddedByIp { get; set; }
        public int ViewCount { get; set; }
        public int AlbumId { get; set; }
        public int Status { get; set; }
        public string UserId { get; set; }
        public string UploadBy { get; set; }
    }

    public class Images
    {
        public static Images Instance
        {
            get { return new Images(); }
        }

        public static ImagesListView LoadDataFromListItem(tbl_Image item)
        {
            var objImages = new ImagesListView
            {
                AddedByIp = item.AddedByIP,
                AlbumId = Convert.ToInt32(item.AlbumID),
                ImageDate = Convert.ToDateTime(item.ImageDate),
                ImageDesc = item.ImageDesc,
                ImageId = item.ImageID,
                ImageName = item.ImageName,
                ImageUrl = item.ImageUrl,
                Status = Convert.ToInt32(item.Status),
                ViewCount = Convert.ToInt32(item.ViewCount),
                UploadBy = item.UploadBy.ToString()
            };
            return objImages;
        }

        public bool Create(string imageName, string imageDesc, string imageUrl, int albumId, string uploadBy)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objImages = new tbl_Image
                    {
                        AddedByIP = HttpContext.Current.Request.UserHostAddress,
                        AlbumID = albumId,
                        ImageDate = DateTime.Now,
                        ImageDesc = imageDesc,
                        ImageName = imageName,
                        ImageUrl = imageUrl,
                        Status = 0,
                        ViewCount = 0,
                        UploadBy = Guid.Parse(uploadBy)
                    };

                    dc.tbl_Images.InsertOnSubmit(objImages);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(string imageName, string imageDesc, string imageUrl, int albumId, int imageId, int status)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objImages = (dc.tbl_Images.Where(p => p.ImageID == imageId)).Single();
                    objImages.ImageName = imageName;
                    objImages.ImageDesc = imageDesc;
                    objImages.ImageUrl = imageUrl;
                    objImages.AlbumID = albumId;
                    objImages.Status = status;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int imageId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objImages = dc.tbl_Images.Where(p => p.ImageID == imageId);

                    dc.tbl_Images.DeleteAllOnSubmit(objImages);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void DeletePhoto(int imageId)
        {
            var item = GetById(imageId);
            var imgView = new ImagesListView
                              {
                                  UploadBy = item.UploadBy,
                                  ImageDate = item.ImageDate,
                                  ImageUrl = item.ImageUrl
                              };

            if (!Delete(imageId)) return;

            var membershipUser = Membership.GetUser(Guid.Parse(imgView.UploadBy));
            if (membershipUser == null) return;

            var sPath = Configs.UserFolder + membershipUser.UserName + "\\" + imgView.ImageDate.Year + "\\" +
                        Common.Instance.Right("0" + imgView.ImageDate.Month, 2) + "\\" +
                        Common.Instance.Right("0" + imgView.ImageDate.Day, 2) + "\\";

            Common.Instance.DeleteFile(sPath + "51x0__" + imgView.ImageUrl);
            Common.Instance.DeleteFile(sPath + "100x0__" + imgView.ImageUrl);
            Common.Instance.DeleteFile(sPath + "135x0__" + imgView.ImageUrl);
            Common.Instance.DeleteFile(sPath + "160x0__" + imgView.ImageUrl);
            Common.Instance.DeleteFile(sPath + "500x0__" + imgView.ImageUrl);
            Common.Instance.DeleteFile(sPath + "640x0__" + imgView.ImageUrl);
            Common.Instance.DeleteFile(sPath + "700x0__" + imgView.ImageUrl);
            Common.Instance.DeleteFile(sPath + "o__" + imgView.ImageUrl);
            Common.Instance.DeleteFile(sPath + imgView.ImageUrl);
        }

        public ImagesListView GetById(int imageId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var objImages = new ImagesListView();
                var query = (from p in dc.tbl_Images where p.ImageID == imageId select p).SingleOrDefault();

                if (query != null)
                {
                    objImages.AddedByIp = query.AddedByIP;
                    objImages.AlbumId = Convert.ToInt32(query.AlbumID);
                    objImages.ImageDate = Convert.ToDateTime(query.ImageDate);
                    objImages.ImageDesc = query.ImageDesc;
                    objImages.ImageId = query.ImageID;
                    objImages.ImageName = query.ImageName;
                    objImages.ImageUrl = query.ImageUrl;
                    objImages.Status = Convert.ToInt32(query.Status);
                    objImages.ViewCount = Convert.ToInt32(query.ViewCount);
                    objImages.UploadBy = query.UploadBy.ToString();
                }

                return objImages;
            }
        }

        public List<ImagesListView> GetByAlbum(int status, int albumId, bool isWhere)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var queryable = from images in dc.tbl_Images
                                join aspnetUser in dc.aspnet_Users on images.UploadBy equals aspnetUser.UserId
                                where !isWhere || (images.Status == status && images.AlbumID == albumId)
                                orderby images.ImageID descending 
                                select new ImagesListView
                                {
                                    AddedByIp = images.AddedByIP,
                                    AlbumId = Convert.ToInt32(images.AlbumID),
                                    ImageDate = Convert.ToDateTime(images.ImageDate),
                                    ImageDesc = images.ImageDesc,
                                    ImageId = images.ImageID,
                                    ImageName = images.ImageName,
                                    ImageUrl = images.ImageUrl,
                                    Status = Convert.ToInt32(images.Status),
                                    ViewCount = Convert.ToInt32(images.ViewCount),
                                    UserId = images.UploadBy.ToString(),
                                    UploadBy = aspnetUser.UserName
                                };

                return queryable.ToList();
            }
        }

        public List<ImagesListView> GetTopImage(int take)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var queryable = from images in dc.tbl_Images
                                join aspnetUser in dc.aspnet_Users on images.UploadBy equals aspnetUser.UserId
                                join tblAlbum in dc.tbl_Albums on images.AlbumID equals tblAlbum.AlbumID
                                where tblAlbum.SiteId == Configs.SiteId orderby images.ImageID descending 
                                select new ImagesListView
                                           {
                                               ImageDate = Convert.ToDateTime(images.ImageDate),
                                               ImageName = images.ImageName,
                                               ImageUrl = images.ImageUrl,
                                               UploadBy = aspnetUser.UserName
                                           };
                return queryable.Take(take).ToList();
                //return queryable.Take(take).ToList().OrderBy(x => Guid.NewGuid()).ToList();
            }
        }

        public List<ImagesListView> GetAll(int status, bool isWhere)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var queryable = from images in dc.tbl_Images
                                join aspnetUser in dc.aspnet_Users on images.UploadBy equals aspnetUser.UserId
                                join tblAlbum in dc.tbl_Albums on images.AlbumID equals tblAlbum.AlbumID
                                where !isWhere || images.Status == status && tblAlbum.SiteId == Configs.SiteId
                                orderby images.ImageID descending 
                                select new ImagesListView
                                           {
                                               AddedByIp = images.AddedByIP,
                                               AlbumId = Convert.ToInt32(images.AlbumID),
                                               ImageDate = Convert.ToDateTime(images.ImageDate),
                                               ImageDesc = images.ImageDesc,
                                               ImageId = images.ImageID,
                                               ImageName = images.ImageName,
                                               ImageUrl = images.ImageUrl,
                                               Status = Convert.ToInt32(images.Status),
                                               UploadBy = aspnetUser.UserName
                                           };
                return queryable.ToList();
            }
        }
    }
}